import axios from 'axios'
import {message} from "ant-design-vue"
import {get} from "@/api/storage/loginStorage";
import router from "@/router";

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 创建axios实例
const service = axios.create({
    // axios中请求配置有baseURL选项，表示请求URL公共部分
    baseURL: '/',
    // 超时
    timeout: 15000
})
// request拦截器
service.interceptors.request.use(config => {
    // 接口验证参数
    const login = get()
    config.headers['Uacc'] = login.account
    config.headers['Upas'] = login.password
    return config
}, error => {
    console.log(error)
    Promise.reject(error)
})

// 响应拦截器
service.interceptors.response.use(res => {
        // 未设置状态码则默认成功状态
        const code = res.data.code || 404
        if (code === 404) {
            router.push({path: '/404'})
            return Promise.reject('error')
        }
        // 获取错误信息
        if (code !== 200) {
            if (res.data.msg) message.error(res.data.msg)
            return Promise.reject('error')
        } else {
            return res.data
        }
    },
    error => {
        console.log('err' + error)
        return Promise.reject(error)
    }
)

export default service
